#include "cc_log.h"
#include "cc_stdio.h"

int cc_writeLog(
	const char* str, 
	const char* fileName,
	int isAppend, 
	int showErrLog
	)
{
	if(!fileName)
	{
		// write log to the standard output screen
		int len = printf(str);
		if(len != strlen(str))
		{
			goto showErr;
		}
	}
	else
	{
		int temp;
		int len;
		// write log to file
		FILE *fp = cc_fopen(fileName, "w");
		if(!fp)
		{
			goto showErr;
		}

		if(isAppend)
		{
			fseek(fp, 0, SEEK_END);
		}
		else
		{
			fseek(fp, 0, SEEK_SET);
		}

		temp = strlen(str);
		len = fwrite(str, 1, strlen(str), fp);
		if(len != strlen(str))
		{
			cc_fclose(fp);
			goto showErr;
		}
	}

	return 1;

showErr:
	printf("\n****cc_writeLog Error occurs!****\n");
	return 0;
}